package com.lenovo.android.syncacc.services;


import android.accounts.AbstractAccountAuthenticator;
import android.accounts.Account;
import android.accounts.AccountAuthenticatorResponse;
import android.accounts.AccountManager;
import android.accounts.NetworkErrorException;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;

import com.lenovo.android.syncacc.activities.MyLoginScreen;

public class MyAccountsAuthenticatorService extends Service {

	private String TAG = "MyAccountsAuthenticatorService";
	private MyAccountsAuthenticator maac;

	@Override
	public IBinder onBind(Intent intent) {
		IBinder binder = null;
		if (intent.getAction().equals(
				AccountManager.ACTION_AUTHENTICATOR_INTENT)) {
			Log.d(TAG, "************* the intent is: "
					+ AccountManager.ACTION_AUTHENTICATOR_INTENT);
			binder = getMyAccountsAuthenticator().getIBinder();
		}
		return binder;
	}

	private MyAccountsAuthenticator getMyAccountsAuthenticator() {
		if (maac == null) {
			maac = new MyAccountsAuthenticator(this);
		}
		return maac;
	}

	class MyAccountsAuthenticator extends AbstractAccountAuthenticator {

		private Context mContext;

		public MyAccountsAuthenticator(Context context) {
			super(context);
			this.mContext = context;
		}

		@Override
		public Bundle addAccount(AccountAuthenticatorResponse response,
				String accountType, String authTokenType,
				String[] requiredFeatures, Bundle options)
				throws NetworkErrorException {
			Bundle bundle = new Bundle();

			Class loginClass = null;
			// loginClass =
			// Class.forName("com.lenovo.android.activites.MyLoginScreen");
			loginClass = MyLoginScreen.class;
			
			Intent intent = new Intent(mContext, loginClass);
			bundle.putParcelable(AccountManager.KEY_INTENT, intent);
			
			intent.putExtra(AccountManager.KEY_ACCOUNT_AUTHENTICATOR_RESPONSE, response);
			
			return bundle;
		}

		@Override
		public Bundle confirmCredentials(AccountAuthenticatorResponse response,
				Account account, Bundle options) throws NetworkErrorException {
			return null;
		}

		@Override
		public Bundle editProperties(AccountAuthenticatorResponse response,
				String accountType) {
			return null;
		}

		@Override
		public Bundle getAuthToken(AccountAuthenticatorResponse response,
				Account account, String authTokenType, Bundle options)
				throws NetworkErrorException {
			return null;
		}

		@Override
		public String getAuthTokenLabel(String authTokenType) {
			return null;
		}

		@Override
		public Bundle hasFeatures(AccountAuthenticatorResponse response,
				Account account, String[] features)
				throws NetworkErrorException {
			return null;
		}

		@Override
		public Bundle updateCredentials(AccountAuthenticatorResponse response,
				Account account, String authTokenType, Bundle options)
				throws NetworkErrorException {
			return null;
		}

	}

}
